package string;

/**
 * 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
 */
public class SpaceSwap {
    public static void main(String[] args) {
        SpaceSwap spaceSwap = new SpaceSwap();
        String s = spaceSwap.replaceSpace("we are familly");
        System.out.println(s.toString());
    }
    public String replaceSpace(String s) {
        int size = 0;
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == ' '){
                size+=3;
            }else {
                size++;
            }
        }
        char[] chars = new char[size];
        int left = s.length() - 1;
        int right = size - 1;
        while (left >= 0){
            if(s.charAt(left) == ' '){
                chars[right--] = '0';
                chars[right--] = '2';
                chars[right--] = '%';
            }else{
                chars[right--] = s.charAt(left);
            }
            left--;
        }
        return String.valueOf(chars);
    }
}
